package com.zhyaoqi.platformautoplug.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.accessibilityservice.AccessibilityServiceInfo;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.view.accessibility.AccessibilityManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zhyaoqi.platformautoplug.R;
import com.zhyaoqi.platformautoplug.util.SpUtils;

import java.util.List;

public class HomeActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView textStatus;
    private ImageView imageStatus;
    private LinearLayout layoutStartPlug,layoutSetting;

    private AccessibilityManager accessibilityManager;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        layoutStartPlug = findViewById(R.id.layout_start_plug);

        layoutSetting = findViewById(R.id.layout_setting);
        textStatus = findViewById(R.id.text_status);
            //开启件
        imageStatus = findViewById(R.id.image_status);
        layoutStartPlug.setOnClickListener(this);
        layoutSetting.setOnClickListener(this);
        SpUtils.initSpUtil(this);

        accessibilityManager = (AccessibilityManager) getSystemService(Context.ACCESSIBILITY_SERVICE);
        accessibilityManager.addAccessibilityStateChangeListener(new AccessibilityManager.AccessibilityStateChangeListener() {
            @Override
            public void onAccessibilityStateChanged(boolean enabled) {
                updateServiceStatus();
            }
        });
        updateServiceStatus();
    }

    private void updateServiceStatus() {
        if(isServiceEnabled()){
            textStatus.setText("关闭插件");
            imageStatus.setBackgroundResource(R.mipmap.ic_stop);
        }else {
            textStatus.setText("开启插件");
            imageStatus.setBackgroundResource(R.mipmap.ic_start);
        }
    }

    private boolean isServiceEnabled(){
        List<AccessibilityServiceInfo> serviceInfos = accessibilityManager.getEnabledAccessibilityServiceList(AccessibilityServiceInfo.FEEDBACK_GENERIC);
        for (AccessibilityServiceInfo info : serviceInfos) {
            if (info.getId() == "$packageName/.service.PlugService") {

                return true;
            }
        }
        return false;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.layout_start_plug:
                try {
                    Toast.makeText(this, "点击插件来开启！", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
                    startActivity(intent);
                }catch (Exception e){
                    e.printStackTrace();
                    Toast.makeText(this, "遇到一些问题,请手动打开系统设置>无障碍服务>自动插件(ฅ´ω`ฅ)", Toast.LENGTH_LONG).show();
                }
                break;
            case R.id.layout_setting:
                startActivity(new Intent(this,SettingActivity.class));
                break;
            default:
                break;
        }
    }
}